package com.yht.weblog.common.utils;

import com.yht.weblog.common.exception.BaseExceptionInterface;
import com.yht.weblog.common.exception.BizException;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author: yht
 * @date: 2023/11/24 21:03
 * @description: 响应参数工具类
 **/
@Data
@Accessors(chain = true)
public class Response<T> implements Serializable {
    // 是否成功，默认为 true
    private boolean success = true;

    // 异常码
    private String errorCode;

    // 响应消息
    private String message;

    // 响应数据
    private T data;

    // =================================== 成功响应 ===================================
    public static <T> Response<T> success(){
        Response<T> response = new Response<>();
        return response;
    }

    public static <T> Response<T> success(T data){
        Response<T> response = new Response<>();
        response.setData(data);
        return response;
    }


    // =================================== 失败响应 ===================================
    public static <T> Response<T> fail(){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        return response;
    }

    public static <T> Response<T> fail(String errorMessage){
        Response<T> response = new Response<>();
        response.setSuccess(false)
                .setMessage(errorMessage);
        return response;
    }

    public static <T> Response<T> fail(String errorCode, String errorMessage){
        Response<T> response = new Response<>();
        response.setSuccess(false)
                .setErrorCode(errorCode)
                .setMessage(errorMessage);
        return response;
    }

    public static <T> Response<T> fail(BizException businessException){
        Response<T> response = new Response<>();
        response.setSuccess(false)
                .setErrorCode(businessException.getErrorCode())
                .setMessage(businessException.getErrorMessage());
        return response;

    }

    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface){
        Response<T> response = new Response<>();
        response.setSuccess(false)
                .setErrorCode(baseExceptionInterface.getErrorCode())
                .setMessage(baseExceptionInterface.getErrorMessage());
        return response;

    }


}
